﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Configuration;
using System.Collections.Concurrent;

namespace YLPublicService
{
    public abstract class ServiceBase<Tuser> : IServiceBase
        where Tuser : new()
    {
        protected static DbDataContext dbContext;
        /// <summary>
        ///  //!++  要让onlineXX 三个同步
        /// </summary>
        protected static ConcurrentDictionary<string, Tuser> onlineUserByLoginName;
        protected static ConcurrentDictionary<Guid, Tuser> onlineUserByGuid;
        protected static ConcurrentDictionary<string, Guid> onlineUserGuidByLoginName;

        protected FTPServerSetting.SettingManager settingManager;
        public ServiceBase()
        {
            settingManager = new FTPServerSetting.SettingManager(ConfigurationManager.AppSettings["FTPConfigPath"]);
        }

        /// <summary>
        /// 数据库中是否存在该用户（普通或者管理员，根据子类各自实现）
        /// 内部直接调用父类tryFindUser即可
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        protected abstract bool TryFindUser(string loginName, string pwd, out Tuser user);
        protected bool isOnline(string loginName)
        {
            return onlineUserByLoginName.ContainsKey(loginName);
        }
        /// <summary>
        /// 除了登录以外都要使用判断
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        protected bool isOnline(Guid guid)
        {
            return onlineUserByGuid.ContainsKey(guid);
        }

        /// <summary>
        /// //Database ServerUser 或者User
        /// </summary>
        protected Tuser getCurrentUser(Guid guid)
        {
            return onlineUserByGuid[guid];
        }
        protected Tuser getCurrentUser(string loginName)
        {
            return onlineUserByLoginName[loginName];
        }


        static ServiceBase()
        {
            dbContext = new DbDataContext();
            onlineUserByLoginName = new ConcurrentDictionary<string, Tuser>();
            onlineUserByGuid = new ConcurrentDictionary<Guid, Tuser>();
            onlineUserGuidByLoginName = new ConcurrentDictionary<string, Guid>();
        }
        private void userOn(dynamic user, out Guid guid)
        {
            onlineUserByLoginName[user.LoginName] = user;
            guid = Guid.NewGuid();
            onlineUserByGuid[guid] = user;
            onlineUserGuidByLoginName[user.LoginName] = guid;
        }
        private void userOff(Guid guid)
        {
            dynamic user = getCurrentUser(guid);
            Tuser t;
            while (onlineUserByLoginName.TryRemove(user.LoginName, out t) == false) ;
            while (onlineUserByGuid.TryRemove(guid, out t) == false) ;
            Guid g;
            while (onlineUserGuidByLoginName.TryRemove(user.LoginName, out g) == false) ;
        }

        /// <summary>
        /// 登录成功返回当前guid,否则返回空字符串
        /// 如果已经登录返回之前的guid
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Guid Login(string loginName, string pwd)
        {
            Tuser u = new Tuser();
            if (TryFindUser(loginName, pwd, out u))
            {
                dynamic user = u;//肯定有以下属性
                if (!isOnline(user.LoginName))
                {
                    Guid guid;
                    userOn(user, out guid);
                    return guid;
                }
                else
                {
                    return onlineUserGuidByLoginName[loginName];
                }
            }
            else
            {
                return Guid.Empty;
            }
        }

        /// <summary>
        /// 注销用户
        /// </summary>
        /// <param name="guid"></param>
        public virtual void LoginOut(Guid guid)
        {
            if (!isOnline(guid))
                return;
            userOff(guid);
        }
    }
}